package com.jason.location.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import com.jason.location.BootApp;
import com.jason.location.R;
import com.jason.location.activity.MainActivity;

/**
 * @ClassName:LocationNotification
 * @Description:定位通知
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015年11月25日09:17:45
 */
public class LocationNotification {

    /**
     * 通知栏Id
     */
    int LOCATION_NOTIFI_ID = 900001;

    String LOCATION_NOTIFI_TAG = "LOCATION_NOTIFI_TAG";

    protected static LocationNotification mNotifiLogic;

    private static final String TAG = LocationNotification.class.getSimpleName();

    /**
     * 通知栏跳转
     */
    private PendingIntent clickPendingIntent;

    /**
     * 通知栏提示
     */
    private Notification mNotifi;

    /**
     * 通知栏管理类
     */
    private NotificationManager mNotifiMgr;

    /**
     * 构造方法
     * [构造简要说明]
     */
    private LocationNotification() {
        mNotifiMgr =
                (NotificationManager) BootApp.mAppContext
                        .getSystemService(Context.NOTIFICATION_SERVICE);
    }

    public Notification getNotification() {
        if (null != mNotifi) {
            return mNotifi;
        }
        return null;
    }

    /**
     * 获取单一实例.
     * [一句话功能简述]<BR>
     * [功能详细描述]
     *
     * @return UMusicDownldNotifiLogic 返回当前类的实例
     */
    public static synchronized LocationNotification getInstance() {
        if (mNotifiLogic == null) {
            mNotifiLogic = new LocationNotification();
        }

        return mNotifiLogic;
    }

    /**
     * 关闭下载中通知栏
     */
    public void cancelDownldNotifi() {
        Log.d(TAG, "cancelDownldNotifi()");
        if (mNotifi != null) {
            mNotifiMgr.cancel(LOCATION_NOTIFI_TAG, LOCATION_NOTIFI_ID);
            mNotifi = null;
        }
    }

    /**
     * 创建通知栏
     */
    public void createNotification(Context context) {
        Log.d(TAG, "createNotification()");
        Intent intent = new Intent(context, MainActivity.class);
        intent.putExtra("fromNotification", true);
        clickPendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        mNotifi =
                new Notification(R.drawable.ic_launcher, "", System.currentTimeMillis());
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.notification_layout);
        remoteViews.setTextViewText(R.id.notification_content, context.getString(R.string.app_name) + context.getString(R.string.locate_starting));
        mNotifi.contentView = remoteViews;
        mNotifi.contentIntent = clickPendingIntent;
        mNotifi.flags = mNotifi.flags | Notification.FLAG_NO_CLEAR;
    }
}
